package com.woniuxy.redis01.service.imple;

import com.woniuxy.redis01.entity.Student;
import com.woniuxy.redis01.mapper.StudentMapper;
import com.woniuxy.redis01.service.SomeService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author 作者
 * @create 2025-06-24 9:40
 */
@Service
public class SomeServiceImpl implements SomeService {

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Override
    public String getDate() {
        String a = null;
        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        if (redisTemplate.hasKey("a")) {
            System.out.println("缓存存在，直接读取缓存数据");
            a = opsForValue.get("a").toString();
        } else {
            System.out.println("缓存不存在，数据库查询，存入缓存");
            a = "aaa";
            opsForValue.set("a", a);
        }
        return a;
    }

//    @Override
//    public void incr1() {
//        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
//        //使用setnx向redis存数据，成功，可以执行业务操作；失败，等待，重试
//        Boolean flag = opsForValue.setIfAbsent("lock", "xxx", 10, TimeUnit.SECONDS);
//        //失败
//        if(!flag){
//            //失败，等待
//            try {
//                Thread.sleep(50);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//            //重试
//            incr1();
//            return;
//        }
//        //成功
//        int num;
//        if (opsForValue.get("num") == null) {
//            num = 1;
//        } else {
//            num = (int) opsForValue.get("num");
//            //取值，+1，村润
//            num++;
//        }
//        opsForValue.set("num", num);
//        //删除锁
//        redisTemplate.delete("lock");
//        System.out.println(num);
//    }

    //Redisson核心类
    @Autowired
    RedissonClient redissonClient;

    @Override
    public void incr1() {
        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
        //获取锁
        RLock lock = redissonClient.getLock("lock");
        //加锁
        lock.lock();
        int num;
        if (opsForValue.get("num") == null) {
            num = 1;
        } else {
            num = (int) opsForValue.get("num");
            //取值，+1，村润
            num++;
        }
        opsForValue.set("num", num);
        //删除锁
        lock.unlock();
        System.out.println(num);
    }



    @Override
    public void incr2() {
        //redis执行指令是单线程
        redisTemplate.opsForValue().increment("num");
    }

    @Override
    public void init() {
        ListOperations<String, Object> opsForList = redisTemplate.opsForList();
        for(int i=1;i<=1000;i++){
            opsForList.leftPush("list", i);
        }
    }

    @Autowired
    StudentMapper studentMapper;

    @Override
    public void second() {
        ListOperations<String, Object> opsForList = redisTemplate.opsForList();
        Object o = opsForList.rightPop("list");
        if(o==null){
            System.out.println("抢购结束");
        }else{
//            System.out.println("抢购成功,添加订单");
            //业务代码，模拟添加订单.订单数据发到消息队列，消费者处理
//            studentMapper.insert(new Student(null, "aaa", 18));
        }
    }
}
